﻿using CodeGenerator.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator.Template.Web.Views
{
    public partial class Add : ITemplate
    {
        public MetaData Data { get; set; }
        public List<LookupClassModel> lookups { get; set; }
        public List<PickupListModel> pickuplist { get; set; }

        public string parent_attr_PK_name = "";
        public string parent_attr_Name_name = "";

        public string parent_class_name = "";

        public string Filename
        {
            get { return "Add{ClassName}.cshtml"; }
        }

        public bool IsGenerateOneTime { get { return false; } }
        public bool IsGenerateFolder { get { return true; } }
        public bool IsRequiredOnlyOneMember { get { return true; } }
        public GenerateStyle GenerateStyle { get { return GenerateStyle.Normal; } }

        // Check if current class model is child class ,if so get parent class info. such as parent class name and attribute pk name
        public bool IsChildClass()
        {
            if (Data.Models[0].ParentClass != null && Data.Models[0].ParentClass.Count > 0)
            {
                parent_class_name = Data.Models[0].ParentClass[0].Name;

                foreach (var attr in Data.Models[0].ParentClass[0]._attributes)
                {
                    if (attr.Pk)
                        parent_attr_PK_name = attr.Name;
                }
                return true;
            }
            else
                return false;
        }

        public String GetPKOfLookup(List<LookupModel> list)
        {
            foreach (LookupModel l in list)
            {
                if (l.DestAttribute.Pk)
                    return l.DestAttribute.Name;
            }
            return "";
        }

        public String GetNameOfLookup(List<LookupModel> list)
        {
            foreach (LookupModel l in list)
            {
                if (!l.DestAttribute.Pk)
                    return l.DestAttribute.Name;
            }
            return "";
        }

        //Calculation Field
        public string IsCalculateOperandReturnFnName(string currentAtt)
        {
            ClassModel Clsmodel = Data.Models[0];
            int a = Clsmodel.Id;
            List<AttributeModel> att = Clsmodel._attributes.FindAll(x => x.FieldType.Name.Equals("Calculation Field"));
            List<string> functions = new List<string>();
            if (att != null)
            {
                foreach (AttributeModel item in att)
                {
                    string[] separators = { "+", "-", "*", "/", "^", "(", ")", " ", ".concat", ".contain", "AddDate", "AddMonth", "AddYear", "Minimum", "Maximun", "[", "]", "DateDiff", ","};
                    string[] words = item.Formula.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                    if (words.Contains(currentAtt))
                    {
                        functions.Add(string.Concat("Calculation", Clsmodel.Name, "_", item.Name, "();")); //function name
                    }
                }
                return string.Join(" ", functions);
            }
            return "";
        }
    }
}
